package com.boot.main.config.redis;

import com.boot.main.utils.Constant;
import com.boot.main.utils.SerializableUtil;
import com.boot.main.utils.StringUtil;
import com.boot.main.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

@Component
public class JedisUtil {
    private static JedisPool jedisPool;
    @Autowired
    public void setJedisPool(JedisPool jedisPool){
        JedisUtil.jedisPool = jedisPool;
    }
    public static synchronized Jedis getJedis(){
        try{
            if(jedisPool != null){
                return jedisPool.getResource();
            }else{
                return null;
            }
        }catch (Exception e){
            throw new CustomException(604, "获取Jedis资源异常:" + e.getMessage());
        }
    }
    public static void closePool() throws CustomException {
        try{
            jedisPool.close();;
        }catch (Exception e){
            throw new CustomException(605, "获取Jedis资源异常:" + e.getMessage());
        }
    }
    public static Object getObject(String key) throws CustomException {
        try(Jedis jedis = jedisPool.getResource()){
            byte[] bytes = jedis.get(key.getBytes());
            if(StringUtil.isNotNull(bytes)){
                return SerializableUtil.unserializable(bytes);
            }
        }catch (Exception e){
            throw new CustomException(606, "获取Redis键值getObject方法异常:key="+key+" cause="+e.getMessage());
        }
        return null;
    }
    public static String setObject(String key, Object value) throws CustomException {
        try(Jedis jedis = jedisPool.getResource()){
            return jedis.set(key.getBytes(), SerializableUtil.serializable(value));
        }catch (Exception e){
            throw new CustomException(607, "设置Redis键值setObject方法异常:key=" + key + " value=" + value + " carse=" + e.getMessage());
        }
    }
    public static String setObject(String key, Object value, int expiretime) throws CustomException {
        String result;
        try(Jedis jedis = jedisPool.getResource()){
            result = jedis.set(key.getBytes(),SerializableUtil.serializable(value));
            if(Constant.OK.equals(result)){
                jedis.expire(key.getBytes(), expiretime);
            }
            return result;
        }catch (Exception e){
            throw new CustomException(608,"设置Redis键值setObject方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
        }
    }
    public static String getJson(String key) throws CustomException {
        try(Jedis jedis = jedisPool.getResource()){
            return jedis.get(key);
        }catch (Exception e){
            throw new CustomException(609,"获取Redis键值getJson方法异常:key=" + key + " cause=" + e.getMessage());
        }
    }
    public static String setJson(String key, String value) throws CustomException {
        try(Jedis jedis = jedisPool.getResource()){
            return jedis.set(key,value);
        }catch (Exception e){
            throw new CustomException(700,"设置Redis键值setJson方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
        }
    }
    public static String setJson(String key,String value, int expiretion) throws CustomException {
        String result;
        try(Jedis jedis = jedisPool.getResource()){
            result = jedis.set(key,value);
            if(Constant.OK.equals(result)){
                jedis.expire(key, expiretion);
            }
            return result;
        }catch (Exception e){
            throw new CustomException(701,"设置Redis键值setJson方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
        }
    }
    public static Long delKey(String key) throws CustomException {
        try(Jedis jedis = jedisPool.getResource()){
            return jedis.del(key.getBytes());
        }catch (Exception e){
            throw new CustomException(702,"删除Redis的键delKey方法异常:key=" + key + " cause=" + e.getMessage());
        }
    }
    public static Boolean exists(String key) throws CustomException {
        try(Jedis jedis = jedisPool.getResource()){
            return jedis.exists(key.getBytes());
        }catch (Exception e){
            throw new CustomException(703,"查询Redis的键是否存在exists方法异常:key=" + key + " cause=" + e.getMessage());
        }
    }
    public static Set<String> keysS(String key) throws CustomException {
        try(Jedis jedis = jedisPool.getResource()){
            return jedis.keys(key);
        }catch (Exception e){
            throw new CustomException(704,"模糊查询Redis的键集合keysS方法异常:key=" + key + " cause=" + e.getMessage());
        }
    }
    public static Set<byte[]> keysB(String key) throws CustomException {
        try(Jedis jedis = jedisPool.getResource()){
            return jedis.keys(key.getBytes());
        } catch (Exception e){
            throw new CustomException(705,"模糊查询Redis的键集合keysB方法异常:key=" + key + " cause=" + e.getMessage());
        }
    }
    public static Long ttl(String key) throws CustomException {
        Long result = -2L;
        try(Jedis jedis = jedisPool.getResource()){
            result = jedis.ttl(key);
            return result;
        }catch (Exception e){
            throw new CustomException(706,"获取Redis键过期剩余时间ttl方法异常:key=" + key + " cause=" + e.getMessage());
        }
    }




















}
